package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.picselect.entity.SelectMediaEntity;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.LocalVideoAdapter;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.bean.MyAlbumBean;
import flc.ast.bean.MyImgBean;
import flc.ast.bean.MyPicLibraryBean;
import flc.ast.databinding.ActivityPicAlbumBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.MhzDateUtils;
import stark.common.basic.event.EventStatProxy;

public class PicAlbumActivity extends BaseAc<ActivityPicAlbumBinding> {

    public static String albumId;
    private SelPictureAdapter selPictureAdapter;
    private List<MyImgBean> listShow = new ArrayList<>();
    private Dialog myDeleteDialog;


    @Override
    protected int onCreate() {
        return R.layout.activity_pic_album;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivPicAlbumBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvPicList.setLayoutManager(new LinearLayoutManager(mContext));
        selPictureAdapter = new SelPictureAdapter();
        selPictureAdapter.setAlbum(true);
        mDataBinding.rvPicList.setAdapter(selPictureAdapter);

        mDataBinding.ivAdd.setOnClickListener(this);
        mDataBinding.ivDelete.setOnClickListener(this);
        //删除弹框
        deleteDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAdd:
                startActivity(AddPicActivity.class);
                break;
            case R.id.ivDelete://删除相册
                myDeleteDialog.show();
                break;
            //删除弹框
            case R.id.ivDialogDeleteAlbumConfirm:
                delete();
                myDeleteDialog.dismiss();
                break;
            case R.id.ivDialogDeleteAlbumCancel:
                myDeleteDialog.dismiss();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        List<String> list = new ArrayList<>();
        List<MyPicLibraryBean> listSp = AppSpUtils.getPictureList();
        if (listSp != null && listSp.size() > 0) {
            for (MyPicLibraryBean bean : listSp) {
                if (bean.getId().equals(albumId)) {
                    mDataBinding.tvPicAlbumTitle.setText(bean.getName());
                    list.addAll(bean.getList());
                    break;
                }
            }
        }
        listShow = MhzDateUtils.getDayData(list);
        if (listShow.size() > 0) {
            selPictureAdapter.setList(listShow);
            mDataBinding.rvPicList.setVisibility(View.VISIBLE);
            mDataBinding.tvPicNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvPicList.setVisibility(View.GONE);
            mDataBinding.tvPicNoData.setVisibility(View.VISIBLE);
        }
    }

    //删除相册
    private void delete() {
        List<MyPicLibraryBean> listSp = AppSpUtils.getPictureList();
        if (listSp != null && listSp.size() > 0) {
            for (int i = 0; i < listSp.size(); i++) {
                if (listSp.get(i).getId().equals(albumId)) {
                    listSp.remove(i);
                    break;
                }
            }
            AppSpUtils.savePictureList(listSp);
            ToastUtils.showShort(getString(R.string.delete_suc));
        }
        finish();
    }


    //删除弹框
    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete_album, null);
        myDeleteDialog.setContentView(inflate);
        myDeleteDialog.setCancelable(true);
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView right = inflate.findViewById(R.id.ivDialogDeleteAlbumConfirm);
        ImageView cancel = inflate.findViewById(R.id.ivDialogDeleteAlbumCancel);
        TextView title = inflate.findViewById(R.id.tvDialogDeleteTitle);
        TextView tip = inflate.findViewById(R.id.tvDialogDeleteTip);
        title.setText(getString(R.string.delete_pic_text1));
        tip.setText(getString(R.string.delete_pic_text2));
        right.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

}